package com.app.utils;

import android.content.SharedPreferences;

import com.app.application.QXApplication;


/**
 * 时间:2015-7-6
 * 公司:DDY
 * 作者:LBH
 * 功能:SharePreferences操作工具类
 */
public class SharePrefUtil {
	@SuppressWarnings("unused")
	private static String tag = SharePrefUtil.class.getSimpleName();
	private final static String SP_NAME = "qx_config";
	private static SharedPreferences sp;

	/**
	 * 是要的Key枚举
	 * @author Administrator
	 *
	 */
	public interface KEY {

		String CITY = "city";										//城市定位成功 城市 默认呼和浩特
		String APP_INFO = "app_info";								//应用配置信息
		String LATITUDE = "latitude";								//纬度
		String LONTITUDE = "lontitude";								//纬度
		String APP_USER = "app_user";								//用户登录信息
		String HOME_ROLL = "home_roll";								//首页轮图
		String HOME_DATA = "home_data";								//首页数据
		String HOME_ACT_MODE = "home_act_recycele_mode";			//首页活动排列方式
		String ACT_MODE = "act_recycele_mode";						//活动排列方式
		String HOME_COURSE_MODE = "home_course_recycele_mode";		//首页课程排列方式
		String COURSE_MODE = "course_recycele_mode";				//课程排列方式
		String APP_INFO_SWITCH = "app_info_switch"; 				//App-消息推送
		String APP_VIDEO_SWITCH = "app_video_switch"; 				//App-视频
		String APP_UPDATE_SWITCH = "app_update_switch"; 			//App-更新
	}

	/**
	 * 保存布尔值
	 * 
	 * @param key
	 * @param value
	 */
	public static void saveBoolean(String key, boolean value) {
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		    sp.edit().putBoolean(key, value).apply();
	}

	/**
	 * 保存字符串
	 * 
	 * @param key
	 * @param value
	 */
	public static void saveString( String key, String value) {
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		sp.edit().putString(key, value).apply();
		
	}
	
	
	/**
	 * 删除存储信息
	 * 
	 * @param key
	 */
	public static void clearString(String key) {
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		sp.edit().putString(key,"").apply();
	}
	
	
	/**
	 * 删除登录存储信息
	 */
	public static void clearLogingSaveData(){
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		sp.edit().putString(KEY.APP_USER, "").apply();

	}
	/**
	 * 全部清除
	 */
	public static void clear(){
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		sp.edit().clear().apply();
	}

	/**
	 * 保存long型
	 * 
	 * @param key
	 * @param value
	 */
	public static void saveLong(String key, long value) {
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		sp.edit().putLong(key, value).commit();
	}

	/**
	 * 保存int型
	 * 
	 * @param key
	 * @param value
	 */
	public static void saveInt(String key, int value) {
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		sp.edit().putInt(key, value).apply();
	}

	/**
	 * 保存float型
	 * 
	 * @param key
	 * @param value
	 */
	public static void saveFloat(String key, float value) {
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		sp.edit().putFloat(key, value).apply();
	}

	/**
	 * 获取字符值
	 * 
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static String getString(String key, String defValue) {
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		return sp.getString(key, defValue);
	}

	/**
	 * 获取int值
	 * 
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static int getInt(String key, int defValue) {
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		return sp.getInt(key, defValue);
	}

	/**
	 * 获取long值
	 * 
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static long getLong(String key, long defValue) {
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		return sp.getLong(key, defValue);
	}

	/**
	 * 获取float值
	 * 
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static float getFloat( String key, float defValue) {
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		return sp.getFloat(key, defValue);
	}

	/**
	 * 获取布尔值
	 * 
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static boolean getBoolean(String key,boolean defValue) {
		if (sp == null)
			sp = QXApplication.getContext().getSharedPreferences(SP_NAME, 0);
		return sp.getBoolean(key, defValue);
	}

}
